//
//  ControlTemplateViewDateController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewDateController.h"

#pragma mark -
#pragma mark 时间控件【日期控件】

@interface ControlTemplateViewDateController () {
    UIView* datebgView;
    CGSize titleSize;
}
@property (nonatomic, retain) NSString* defaultValue;

@end

@implementation ControlTemplateViewDateController

static ControlTemplateViewDateController* sharedManagerDateInter = nil;

+ (ControlTemplateViewDateController*)sharedManagerDate
{
    sharedManagerDateInter = [[self alloc] init];
    
    return sharedManagerDateInter;
}

/**
 *  日期变量初始化
 */
- (void)initVar
{
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isNeedAdpterHotspot = YES;
    
    [self initVar];
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [titleLabel extendAttributedString:[_params valueForKey:@"title"] lineSpace:LINESPACE_6 font:kFont_16];
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    titleSize = [[_params valueForKey:@"title"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:TITLE_WIDTH];
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"attrValue"] length] > 0) {
        _defaultValue = [_params valueForKey:@"attrValue"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    ////////是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    
    ////////是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) {
        UILabel* contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
        [contentLabel setText:[_params valueForKey:@"attrValue"]];
        [contentLabel setFont:kFont_16];
        [contentLabel setTextColor:kColor_1];
        [_bgMainView addSubview:contentLabel];
        
        //标题长度换行
        CGSize sizeTitle = STRING_SIZE_FONT(TITLE_WIDTH, [_params valueForKey:@"title"], 16);
        titleLabel.frame = CGRectMake(HEADSPACE, (CGRectGetHeight(_bgMainView.bounds) - sizeTitle.height) / 2, TITLE_WIDTH, sizeTitle.height);
        
    }
    else {
        CustomButtonView* abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), TEMPLATE_HEIGHT)];
        abtn.tempData = _params;
        [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:abtn];
        [abtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_bgMainView);
        }];
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
        UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(_bgMainView.bounds) - image.size.width - HEADSPACE, (CGRectGetHeight(_bgMainView.bounds) - image.size.height) / 2, image.size.width, image.size.height)];
        [iconImageView setImage:image];
        [_bgMainView addSubview:iconImageView];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_bgMainView.mas_centerY);
            make.right.equalTo(_bgMainView.mas_right).offset(-HEADSPACE);
        }];
        
        //显示值
        UILabel* valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT - image.size.width - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
        [valueLabel setFont:kFont_16];
        [valueLabel setTextColor:kColor_C];
        [valueLabel setText:[_params valueForKey:@"markedWords"]];
        [valueLabel setTextAlignment:NSTextAlignmentLeft];
        [abtn addSubview:valueLabel];
        
        titleSize.height = titleSize.height > 24 ? titleSize.height : 24;
        valueLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT - image.size.width - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT -24 + titleSize.height);
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(valueLabel.bounds));
        UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
        lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
    }
    
}

/**
 *  打开日期选择框
 *
 *  @param sender 按钮实例
 */
- (void)onBtnAction:(CustomButtonView*)sender
{
    CGSize size = [[UIScreen mainScreen] bounds].size;
    datebgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    datebgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[[UIApplication sharedApplication] keyWindow] addSubview:datebgView];
    
    DatePickerView* datePickerView = [[DatePickerView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - 250, CGRectGetWidth(self.view.bounds), 250)];
    datePickerView.delegate = self;
    NSDate* now = [NSDate date];
    [datePickerView.datePicker setDate:now animated:YES];
    datePickerView.datePicker.minimumDate = now;
    [[[UIApplication sharedApplication] keyWindow] addSubview:datePickerView];
}

//时间回调
#pragma mark datapicker delegate
- (void)didDatePickerViewCancelClicked:(DatePickerView*)aPickerView
{
    [datebgView removeFromSuperview];
    [aPickerView removeFromSuperview];
}

/**
 *  日期选择确定
 *
 *  @param aPickerView 选择框实例
 */
- (void)didDatePickerViewDoneClicked:(DatePickerView*)aPickerView
{
    [datebgView removeFromSuperview];
    
    NSDate* dateStr = [aPickerView selectDate];
    
    NSString* dateString = [SEGUtils stringDateByFormatString:@"yyyy-MM-dd" withDate:dateStr];
    
    for (CustomButtonView* abtn in _bgMainView.subviews) {
        if ([abtn isKindOfClass:[CustomButtonView class]]) {
            UILabel* alabel = [abtn.subviews objectAtIndex:0];
            [alabel setText:dateString];
            alabel.textColor = kColor_1;
        }
    }
    
    _valueStr = dateString;
    _optional = @"0";
    
    [aPickerView removeFromSuperview];
}

/**
 *  给日期填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingDateView:(NSString*)valueStr
{
    _valueStr = valueStr;
    if ([_valueStr length] > 0) {
        for (CustomButtonView* abtn in _bgMainView.subviews) {
            if ([abtn isKindOfClass:[CustomButtonView class]]) {
                UILabel* alabel = [abtn.subviews objectAtIndex:0];
                [alabel setText:_valueStr];
                alabel.textColor = kColor_1;
            }
        }
        _optional = @"0";
    }
    else {
        _optional = @"1";
    }
}

@end


